package com.ycsd.invest.order.common.utils;

import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;
import kd.bos.dataentity.metadata.IDataEntityProperty;
import kd.bos.dataentity.metadata.IDataEntityType;
import kd.bos.dataentity.metadata.clr.CollectionProperty;
import kd.bos.dataentity.metadata.clr.ComplexProperty;

import java.util.HashMap;

public class DynamicObjectUtil {

    public static HashMap<String, Object> readPropValue(DynamicObject dataEntity){
        if (dataEntity == null) return null;
        HashMap<String, Object> map = new HashMap<>();
        // 获取数据包对应的实体模型
        IDataEntityType dType = dataEntity.getDataEntityType();

        for(IDataEntityProperty property : dType.getProperties()){
            if (property instanceof CollectionProperty){
                // 集合属性，关联子实体，值是数据包集合
                DynamicObjectCollection rows = dataEntity.getDynamicObjectCollection(property);
                // 递归读取子实体各行的属性值
                for (DynamicObject row : rows){
                    readPropValue(row);
                }
            }
            else if (property instanceof ComplexProperty){
                // 复杂属性，关联引用的基础资料，值是另外一个数据包
                DynamicObject refDataEntity = dataEntity.getDynamicObject(property);
                // 递归读取引用的基础资料属性值
                if (refDataEntity != null){
                    readPropValue(refDataEntity);
                }
            }
            else {
                // 简单属性，对应普通的字段，值是文本、数值、日期、布尔等
                Object propValue = dataEntity.get(property);

                // 输出" 属性名 = 属性值 "
//                String msg = String.format("%s = %s", property.getName(), propValue);
                map.put(property.getName(), propValue);
            }
        }
        return map;
    }
}
